package com.liao.badminton.handler.encode;

import com.liao.badminton.code.util.ObjectSerialUtil;
import com.liao.badminton.dto.SystemUsersDto;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.log4j.Log4j2;

import java.util.List;

@Log4j2
public class TransferStandardDecode extends ByteToMessageDecoder {
    Integer i = 0;

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out) {

        if (byteBuf.readableBytes() >= 4) {
            if (i == 0) {
                i = byteBuf.readInt();
            }
            if (byteBuf.readableBytes() < i || i < 0) {
                log.info("当前可读数据不够，继续等待。。");
                return;
            }
            byte[] b = new byte[i];
            if (byteBuf.readableBytes() >= i) {
                byteBuf.readBytes(b);
                SystemUsersDto transferStandard = ObjectSerialUtil.deserializer(b, SystemUsersDto.class);
                out.add(transferStandard);
            }
            i = 0;
        }
    }

}
